package biz;

import dao.ProductDao;

import java.util.Map;

public class ProductBiz {

    private ProductDao dao = new ProductDao();

    // 添加商品判断
    public int saveProduct(String name, String num, String tname,
                           String price, String image, String message,String recommend) throws BizException {

        isMSData(name, num, tname, price, image, message,recommend);

        Map<String,Object> map = dao.selectTypeT(tname);
        if (map==null){
            throw new BizException("该类型不存在");
        }
        String tid = map.get("tid").toString();
        return dao.saveProduct(name, num, tid, price, image, message,recommend);
    }

    // 修改商品判断
    public int modifyProduct(String id, String name, String num, String tname,
                           String price, String image, String message,String recommend) throws BizException {

        isMSData(name, num, tname, price, image, message,recommend);

        Map<String,Object> map = dao.selectTypeT(tname);
        if (map==null){
            throw new BizException("该类型不存在");
        }
        String tid = map.get("tid").toString();
        return dao.modifyProduct(id, name, num, tid, price, image, message, recommend);
    }

    // 商品修改或添加 数据判断
    private void isMSData(String name, String num, String tname,
                          String price, String image, String message,String recommend) throws BizException {

        if (name == null || "".equals(name)){
            throw new BizException("商品名称不能为空！");
        }



        if (tname == null || "".equals(tname)){
            throw new BizException("商品类型不能为空！");
        }


        if (price == null || price.trim().isEmpty()){
            throw new BizException("商品价格不能为空");
        }
        if (price.matches("(\\d+)")==false){
            throw new BizException("商品价格不能为非数字字符！");
        }

        if (num == null || "".equals(num)){
            throw new BizException("商品数量不能为空！");
        }

        if (recommend == null || recommend.trim().isEmpty() == true){
            throw new BizException("商品推荐不能为空！");
        }

        if (message == null || "".equals(message)){
            throw new BizException("商品介绍不能为空！");
        }

        if (image == null || "".equals(image)){
            throw new BizException("图片不能为空！");
        }



    }
}
